#!/usr/bin/python3

import requests
import os
import re

url = "https://www.ximalaya.com/revision/play/v1/show?id=19110746&num=1&sort=0&size=30&ptype=0"

req = requests.session()

# 访问目录页
webUrl = "https://www.ximalaya.com/jiaoyu/19110746/"
headers = {
    "Host": "www.ximalaya.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    }
r = req.get(webUrl, headers=headers)
result = r.text
# print(result)
fileResult = re.findall('class="page-link _Xo"(.*?)>', result)
pagelist = []
for i in fileResult:
    split = i.split("/")
    pStr = split[len(split) - 2]
    if pStr.find('p') == 0:
        page = pStr.replace("p", "")
        print(page)
        pagelist.append(int(page))
# print(fileResult)
maxPage = max(pagelist)
print("最大页码:", maxPage)

# if 1 > 0:
#     exit()


headers = {
    "Host": "www.ximalaya.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
    "Accept": "*/*",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "xm-sign": "aec71c5e1158c343780ed6400b5453e2(27)1579179223520(4)1579179078903",
    "Connection": "keep-alive",
    "Referer": "https://www.ximalaya.com/youshengshu/25338717/",
    "Cookie": "_xmLog=xm_k59fd3cfi4aql8; device_id=xm_1578737195570_k59fd4c2ohw6lr; Hm_lvt_4a7d8ec50cfd6af753c4f8aee3425070=1578737196,1579178334; 1&remember_me=y; 1&_token=59436631&D1FB179D840E48098B4A625DF6B7CF63NdV954A47712B887774644AB40B95EC8F11AD38191A88B4468A59D8E3A4C5EA1AD1; s&e=081d1b600afa395d53dcd767b3120408; x_xmly_traffic=utm_source%253A%2526utm_medium%253A%2526utm_campaign%253A%2526utm_content%253A%2526utm_term%253A%2526utm_from%253A; Hm_lpvt_4a7d8ec50cfd6af753c4f8aee3425070=1579178482; s&a=JS^%07_%05ASJ%0A%05%02YZN%07O%5C%0B%04%0AXAX%1C%5C^U^WK[V[CSBRCOK^XYBXM["
    }
req.headers = headers
r = requests.get(url, headers=headers)
print(r.text)

resultJson = r.json()

items = resultJson["data"]["tracksAudioPlay"]

audioUrlPre = "https://www.ximalaya.com/revision/play/v1/audio?ptype=1&id="
trackIdList = []
audioUrlHead = {
    "Host": "fdfs.xmcdn.com",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1"
}
base_dir = 'D:/test\喜马拉雅'
for i in items:
    albumName = i["albumName"]
    trackName = i["trackName"]
    trackName = trackName.replace("（您的随手点赞！订阅！是对主播最大的支持）", "")
    trackId = i["trackId"]
    getAudioUrl = audioUrlPre + str(trackId)
    r = req.get(getAudioUrl)
    audioJson = r.json()
    downUrl = audioJson["data"]["src"]

    file_dir = base_dir + "/" + albumName
    if os.path.exists(file_dir) is False:
        os.mkdir(file_dir)

    split = downUrl.split(".")
    suffix = ".m4a"
    if len(split) > 1:
        suffix = "." + split[len(split) - 1]
    file_path = file_dir + "/" + trackName + suffix
    print(downUrl)
    print(file_path)
    try:
        # audioUrlHead["Referer"] = downUrl
        r = req.get(downUrl, headers=audioUrlHead)
        with open(file_path, 'wb') as f:
            f.write(r.content)
            f.flush()
    except:
        print("request error url:" + downUrl)



# print(trackIdList)
#
# print("url:")
# for trackId in trackIdList:
#     getAudioUrl = audioUrlPre + str(trackId)
#     r = req.get(getAudioUrl)
#     audioJson = r.json()
#     print(audioJson["data"]["src"])
